<!--
 * @Description:对联悬浮框
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-20 22:26:19
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>对联悬浮框</title>
	<style>
		#div1 {
			width: 100px;
			height: 150px;
			background: green;
			position: absolute;
			right: 0;
			bottom: 0;
		}

		body {
			height: 2000px;
		}
	</style>
	<script>
		window.onscroll = function () {
			var odiv = document.getElementById("div1")
			let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			let sp = (document.documentElement.clientHeight - odiv.offsetHeight)/2 + scrollTop
			// startmove(sp 
			// console.log("px", "=", scrollTop, "=", odiv.style.top,
			// 	"1p", document.documentElement.clientHeight,
			// 	"2p", odiv.offsetHeight,
			// 	"3p", scrollTop
			// )
			startmove(Math.ceil(sp))
		}
		var timer = null;
		function startmove(itarget) {
			var odiv = document.getElementById("div1")
			clearInterval(timer);
			timer = setInterval(() => {
				console.log("itarget33", itarget)
				var speed = (itarget - odiv.offsetTop) / 6
				speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
				if (odiv.offsetTop == itarget) {
					clearInterval(timer)
				} else {
					console.log("odiv.offsetTop", odiv.offsetTop)
					console.log("speed", speed)
					odiv.style.top = odiv.offsetTop + speed + 'px'
				}
			}, 30)
		}
	</script>
</head>

<body>
	<!-- <div>
		Math.ceil(-9.8) 向上 -9 <br>
		Math.floor(4.8) 向下 4 <br>
		速度=（目标值-当前值）/缩放比例 <br>
		缓冲运动不忘了取整
	</div> -->
	<div id="div1">
		<span>分享到</span>
	</div>
</body>

</html>